﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace Sogeti.Utils
{
  public class SharePointListWrapper
  {

    public string ServerUrl { get; set; }
    public string RelativeSiteUrl { get; set; }
    public string ListName { get; set; }
    public string ViewName { get; set; }


    private SPSite _site;
    private SPWeb _web;
    private SPList _List;



    public SharePointListWrapper(string serverUrl, string relativeSiteUrl, string reqListName, string pendingView)
    {
      ServerUrl = serverUrl;
      RelativeSiteUrl = relativeSiteUrl;
      ListName = reqListName;
      ViewName = pendingView;
    }

    public SharePointListWrapper(string serverUrl, string relativeSiteUrl, string reqListName)
    {
      ServerUrl = serverUrl;
      RelativeSiteUrl = relativeSiteUrl;
      ListName = reqListName;
    }

    public SPList SPList
    {
      get
      {
        if (_List == null)
        {
          _site = new SPSite(ServerUrl);
          _web = _site.AllWebs[RelativeSiteUrl];

          if (IsGuid(ListName))
          {
            Guid g = new Guid(ListName);
            _List = _web.Lists[g];
          }
          else
          {
            _List = _web.Lists[ListName];

          }
        }
        return _List;
      }
    }

    private bool IsGuid(string s)
    {
      try
      {
        Guid g = new Guid(s);
        return true;
      }
      catch (Exception ex)
      {
        return false;
      }
    }


  }

}
